package com.kmxd.ams.app.system;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmxd.ams.client.system.ISysNoticeTypeSvc;
import com.kmxd.ams.client.system.dto.SysNoticeTypeAddDTO;
import com.kmxd.ams.client.system.dto.SysNoticeTypeEditDTO;
import com.kmxd.ams.client.system.dto.SysNoticeTypeQueryDTO;
import com.kmxd.ams.client.system.vo.SysNoticeTypeListVO;
import com.kmxd.ams.client.system.vo.SysNoticeTypeVO;
import com.kmxd.ams.core.common.query.QueryGen;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.core.util.SecurityUtil;
import com.kmxd.ams.infra.system.entity.SysNoticeType;
import com.kmxd.ams.infra.system.mapper.SysNoticeTypeMapper;
import java.util.List;
import java.util.Set;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 通知类型 服务实现类
 *
 * @author nickbi
 * @since 2023-04-07
 */
@Service
@AllArgsConstructor
public class SysNoticeTypeSvcImpl extends ServiceImpl<SysNoticeTypeMapper, SysNoticeType>
    implements ISysNoticeTypeSvc {

  private SysNoticeTypeMapper sysNoticeTypeMapper;

  @Override
  public Page<SysNoticeTypeListVO> selectPage(Page reqPage, SysNoticeTypeQueryDTO req) {
    LambdaQueryWrapper<SysNoticeType> queryWrapper =
        QueryGen.init(new QueryWrapper<SysNoticeType>(), req).lambda();
    queryWrapper.orderByDesc(SysNoticeType::getSort);
    Page<SysNoticeType> page = sysNoticeTypeMapper.selectPage(reqPage, queryWrapper);
    Page<SysNoticeTypeListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    voPage.setRecords(BeanUtil.copyToList(page.getRecords(), SysNoticeTypeListVO.class));
    return voPage;
  }

  @Override
  public List<SysNoticeTypeListVO> selectList(SysNoticeTypeQueryDTO req) {
    LambdaQueryWrapper<SysNoticeType> queryWrapper =
        QueryGen.init(new QueryWrapper<SysNoticeType>(), req).lambda();
    queryWrapper.orderByDesc(SysNoticeType::getSort);
    List<SysNoticeType> list = sysNoticeTypeMapper.selectList(queryWrapper);
    return BeanUtil.copyToList(list, SysNoticeTypeListVO.class);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(SysNoticeTypeAddDTO req) {
    SysNoticeType entity = BeanUtil.copyProperties(req, SysNoticeType.class);
    entity.setComId(SecurityUtil.getCropId());
    sysNoticeTypeMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(SysNoticeTypeEditDTO req) {
    SysNoticeType entity = BeanUtil.copyProperties(req, SysNoticeType.class);
    entity.setComId(SecurityUtil.getCropId());
    sysNoticeTypeMapper.updateById(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(String ids) {
    Set<Long> idList = IdUtil.split(ids);
    this.baseMapper.deleteBatchIds(idList);
  }

  @Override
  public SysNoticeTypeVO view(Long id) {
    SysNoticeType entity = sysNoticeTypeMapper.selectById(id);
    return BeanUtil.copyProperties(entity, SysNoticeTypeVO.class);
  }
}
